package com.uenpay.im.ui.old.activity.crowdfund;

import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.uenpay.im.invokeitems.HttpRequestHandle;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-7 下午6:57:27
 * @version 1.0
 * @function TODO
 */
public class CrowdFundHandle {

	public interface ICreateCrowdFundLisenter {
		void onCreateCFResult(int status, String msg,
											  String crowdFundId, String amount);
	}

	/**
	 * 发起众筹（江湖救急）
	 * 
	 * @param chatid
	 * @param amount
	 * @param memo
	 * @param mICreateCrowdFundLisenter
	 */
	public static void createCrowdFund(String chatid, String amount,
			String memo,
			final ICreateCrowdFundLisenter mICreateCrowdFundLisenter) {
		CreateCrowdfundInvokItem item = new CreateCrowdfundInvokItem(chatid,
				amount, memo);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							CreateCrowdfundInvokItem.CreateCrowdfundInvokItemResult result = ((CreateCrowdfundInvokItem) resultObject)
									.getOutput();
							if (result != null) {
								mICreateCrowdFundLisenter.onCreateCFResult(
										result.status, result.msg,
										result.crowdFundId, result.amount);
							} else {
								mICreateCrowdFundLisenter.onCreateCFResult(-1,
										failReason, "", "");
							}
						} else {
							mICreateCrowdFundLisenter.onCreateCFResult(-1,
									failReason, "", "");
						}
					}
				});
	}

	public interface ISosTransferListener {
		void onSosTransferResult(int status, String msg);
	}

	/**
	 * 众筹转账（江湖救急）
	 * 
	 * @param paypassword
	 * @param amount
	 * @param memo
	 */
	public static void sosTransfer(String fundid, String paypassword,
			String amount, String memo,
			final ISosTransferListener mISosTransferListener) {

		SOSTransferInvokItem item = new SOSTransferInvokItem(fundid,
				paypassword, amount, memo);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							SOSTransferInvokItem.SOSTransferInvokItemResult result = ((SOSTransferInvokItem) resultObject)
									.getOutput();
							if (result != null) {
								mISosTransferListener.onSosTransferResult(
										result.status, result.msg);
							} else {
								mISosTransferListener.onSosTransferResult(-1,
										failReason);
							}
						} else {
							mISosTransferListener.onSosTransferResult(-1,
									failReason);
						}
					}
				});

	}

	public interface IGetCrowdFundDetailListener {
		void onGetCrowdFundDetailResult(
				GetCrowdfundInfoInvokItem.GetCrowdfundInfoInvokItemResult mGetCrowdfundInfoInvokItemResult);
	}

	/**
	 * 众筹详情（江湖救急）
	 * 
	 * @param fundid
	 */
	public static void getCrowdFundDetail(String fundid,
			final IGetCrowdFundDetailListener mIGetCrowdFundDetailListener) {

		GetCrowdfundInfoInvokItem item = new GetCrowdfundInfoInvokItem(fundid);
		HttpRequestAsyncTaskQueue
				.getInstance()
				.AddTask(
						new HttpTextAsyncTask(item)
								.SetRequestType(HttpRequestType.SERVER_FAIL_AND_CACHE),
						new OnHttpRequestTextListener() {

							@Override
							public void OnFail(boolean isCache, String reason) {
								mIGetCrowdFundDetailListener
										.onGetCrowdFundDetailResult(null);
							}

							@Override
							public void OnProgress(long total_bytes,
									long downloaded_bytes) {
							}

							@Override
							public void OnSuccess(HttpInvokeItem item,
									boolean isCache, String cacheFilePath) {
								GetCrowdfundInfoInvokItem.GetCrowdfundInfoInvokItemResult result = ((GetCrowdfundInfoInvokItem) item)
										.getOutput();
								mIGetCrowdFundDetailListener
										.onGetCrowdFundDetailResult(result);
							}
						});

	}

}
